package fr.unice.servlet;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gdata.client.youtube.YouTubeService;
import com.google.gdata.data.Entry;
import com.google.gdata.data.Feed;
import com.google.gdata.data.Person;
import com.google.gdata.data.TextContent;
import com.google.gdata.data.extensions.Comments;
import com.google.gdata.data.youtube.VideoEntry;
import com.google.gdata.util.ServiceException;

import fr.unice.outils.Panier;
import fr.unice.outils.PanierBean;

public class Recapitulatif extends HttpServlet {
        public void init() {
        }

        public void doPost(HttpServletRequest req, HttpServletResponse rep) {
                this.doGet(req, rep);
        }

        public void doGet(HttpServletRequest req, HttpServletResponse rep) {
                Panier panierUser = null;
                PanierBean panier = (PanierBean) this.getServletContext().getAttribute(
                                "panier");
                panierUser = new Panier(panier.getLesTag(), panier.getTagSelection(),
                                panier.getTagCpt(), panier.getVideoList(), panier
                                                .getSuggestion());
                ArrayList<VideoEntry> lesVideo = panierUser.getVideoList();
                YouTubeService myService = new YouTubeService(
                                "ytapi-CedricDebeaume-acb-2ldn3hca-0",
                                "AI39si4hrTZVAAS1IOV90AWq5oK3M88DvnolhRxJenj7Nly2J4NCwXXs0zXwoF-U6rO3jn9hQvZl_qcK4HNGCINo6Qpbn_ZYdQ");
        PrintWriter writer;
        String path =this.getServletContext().getRealPath(req.getContextPath());

                try {


                        writer = new PrintWriter(new BufferedWriter(new FileWriter(
                                        path+"fichier.dot", false)));
                        writer.print("");
                        writer.write("digraph G {");
                        writer.write("edge [color=red, arrowsize=2];node [color=lightyellow2, style=filled];");
                        //recuperer toutes les videos et ajouter au fichier dot les lien user video!
                        ArrayList<String> ligne = new ArrayList<String>();
                        String auth="";
                        for (int i = 0; i < lesVideo.size(); i++) {
                                List<Person> authors = lesVideo.get(i).getAuthors();

                                for(Person author: authors){
                                        auth = "video"+i;
                                }
                                Comments comments = lesVideo.get(i).getComments();
                                if (comments != null && comments.getFeedLink() != null) {
                                        Feed feed = myService.getFeed(new URL(comments.getFeedLink().getHref()+"?max-results=100&start-index=1"), Feed.class);
                                        for (Entry e : feed.getEntries()) {
                                                //pour chacun des utilisateur parcourir la liste de ces video pour savoir si on(un utilisateur rencontré) a commenté dessus
                                                List<Person> pers = e.getAuthors();

                                                for(Person per: pers){
                                                        if(!ligne.contains(per.getName()+" -> "+auth+";")){
                                                        ligne.add(per.getName()+" -> "+auth+";");
                                                        writer.write(per.getName()+" -> "+auth+";");
                                                        }
                                                }
                                        }
                                }
                                ligne.clear();

                        }
                        writer.write("}");
                        writer.close();

                        String script="\""+path+"\\bin\\fdp\" -Tpng \""+path+"fichier.dot\" -o \""+path+"fichier.png\"";
                        Runtime.getRuntime().exec(script);
                        try {
                                Thread.sleep(4000);
                        } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                        rep.sendRedirect("result.jsp");
                } catch (IOException  e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch(ServiceException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }

        }

}
